{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "西瓜书习题3.4:选择两个UCI数据集，比较10折交叉验证法和留一法所估计出的对率回归的错误率。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "载入数据集，数据集来源：http://archive.ics.uci.edu/ml/datasets/Autism+Screening+Adult\n",
    "\n",
    "@para   path：数据集路径\n",
    "@return attribute,data：数据集中的属性和数据\n",
    "'''\n",
    "def load_data(path):\n",
    "    if path.find('.arff') <0:\n",
    "        print('the file is nott .arff file')\n",
    "        return\n",
    "    f = open(path)\n",
    "    lines = f.readlines()\n",
    "    data = []\n",
    "    attribute = []\n",
    "    for i in lines[:24]:\n",
    "        temp = i.split(' ')\n",
    "        if(temp[0]=='@attribute'):\n",
    "            attribute.append(temp[1])\n",
    "    for i in lines[25:]:\n",
    "        temp = i.split(',')\n",
    "        data.append(temp)\n",
    "    return attribute,data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A1_Score</th>\n",
       "      <th>A2_Score</th>\n",
       "      <th>A3_Score</th>\n",
       "      <th>A4_Score</th>\n",
       "      <th>A5_Score</th>\n",
       "      <th>A6_Score</th>\n",
       "      <th>A7_Score</th>\n",
       "      <th>A8_Score</th>\n",
       "      <th>A9_Score</th>\n",
       "      <th>A10_Score</th>\n",
       "      <th>...</th>\n",
       "      <th>gender</th>\n",
       "      <th>ethnicity</th>\n",
       "      <th>jundice</th>\n",
       "      <th>austim</th>\n",
       "      <th>contry_of_res</th>\n",
       "      <th>used_app_before</th>\n",
       "      <th>result</th>\n",
       "      <th>age_desc</th>\n",
       "      <th>relation</th>\n",
       "      <th>Class/ASD</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>f</td>\n",
       "      <td>White-European</td>\n",
       "      <td>no</td>\n",
       "      <td>no</td>\n",
       "      <td>'United States'</td>\n",
       "      <td>no</td>\n",
       "      <td>6</td>\n",
       "      <td>'18 and more'</td>\n",
       "      <td>Self</td>\n",
       "      <td>NO\\n</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>m</td>\n",
       "      <td>Latino</td>\n",
       "      <td>no</td>\n",
       "      <td>yes</td>\n",
       "      <td>Brazil</td>\n",
       "      <td>no</td>\n",
       "      <td>5</td>\n",
       "      <td>'18 and more'</td>\n",
       "      <td>Self</td>\n",
       "      <td>NO\\n</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>m</td>\n",
       "      <td>Latino</td>\n",
       "      <td>yes</td>\n",
       "      <td>yes</td>\n",
       "      <td>Spain</td>\n",
       "      <td>no</td>\n",
       "      <td>8</td>\n",
       "      <td>'18 and more'</td>\n",
       "      <td>Parent</td>\n",
       "      <td>YES\\n</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>f</td>\n",
       "      <td>White-European</td>\n",
       "      <td>no</td>\n",
       "      <td>yes</td>\n",
       "      <td>'United States'</td>\n",
       "      <td>no</td>\n",
       "      <td>6</td>\n",
       "      <td>'18 and more'</td>\n",
       "      <td>Self</td>\n",
       "      <td>NO\\n</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>f</td>\n",
       "      <td>?</td>\n",
       "      <td>no</td>\n",
       "      <td>no</td>\n",
       "      <td>Egypt</td>\n",
       "      <td>no</td>\n",
       "      <td>2</td>\n",
       "      <td>'18 and more'</td>\n",
       "      <td>?</td>\n",
       "      <td>NO\\n</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 21 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  A1_Score A2_Score A3_Score A4_Score A5_Score A6_Score A7_Score A8_Score  \\\n",
       "0        1        1        1        1        0        0        1        1   \n",
       "1        1        1        0        1        0        0        0        1   \n",
       "2        1        1        0        1        1        0        1        1   \n",
       "3        1        1        0        1        0        0        1        1   \n",
       "4        1        0        0        0        0        0        0        1   \n",
       "\n",
       "  A9_Score A10_Score    ...    gender       ethnicity jundice austim  \\\n",
       "0        0         0    ...         f  White-European      no     no   \n",
       "1        0         1    ...         m          Latino      no    yes   \n",
       "2        1         1    ...         m          Latino     yes    yes   \n",
       "3        0         1    ...         f  White-European      no    yes   \n",
       "4        0         0    ...         f               ?      no     no   \n",
       "\n",
       "     contry_of_res used_app_before result       age_desc relation Class/ASD  \n",
       "0  'United States'              no      6  '18 and more'     Self      NO\\n  \n",
       "1           Brazil              no      5  '18 and more'     Self      NO\\n  \n",
       "2            Spain              no      8  '18 and more'   Parent     YES\\n  \n",
       "3  'United States'              no      6  '18 and more'     Self      NO\\n  \n",
       "4            Egypt              no      2  '18 and more'        ?      NO\\n  \n",
       "\n",
       "[5 rows x 21 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "path = './Autism-Adult-Data.arff'\n",
    "attribute, data = load_data(path)\n",
    "df = pd.DataFrame(data=data,index=None,columns=attribute)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(704, 21)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A1_Score           0\n",
       "A2_Score           0\n",
       "A3_Score           0\n",
       "A4_Score           0\n",
       "A5_Score           0\n",
       "A6_Score           0\n",
       "A7_Score           0\n",
       "A8_Score           0\n",
       "A9_Score           0\n",
       "A10_Score          0\n",
       "age                0\n",
       "gender             0\n",
       "ethnicity          0\n",
       "jundice            0\n",
       "austim             0\n",
       "contry_of_res      0\n",
       "used_app_before    0\n",
       "result             0\n",
       "age_desc           0\n",
       "relation           0\n",
       "Class/ASD          0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#统计空值个数\n",
    "df.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A1_Score</th>\n",
       "      <th>A2_Score</th>\n",
       "      <th>A3_Score</th>\n",
       "      <th>A4_Score</th>\n",
       "      <th>A5_Score</th>\n",
       "      <th>A6_Score</th>\n",
       "      <th>A7_Score</th>\n",
       "      <th>A8_Score</th>\n",
       "      <th>A9_Score</th>\n",
       "      <th>A10_Score</th>\n",
       "      <th>...</th>\n",
       "      <th>gender</th>\n",
       "      <th>ethnicity</th>\n",
       "      <th>jundice</th>\n",
       "      <th>austim</th>\n",
       "      <th>contry_of_res</th>\n",
       "      <th>used_app_before</th>\n",
       "      <th>result</th>\n",
       "      <th>age_desc</th>\n",
       "      <th>relation</th>\n",
       "      <th>Class/ASD</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>704</td>\n",
       "      <td>704</td>\n",
       "      <td>704</td>\n",
       "      <td>704</td>\n",
       "      <td>704</td>\n",
       "      <td>704</td>\n",
       "      <td>704</td>\n",
       "      <td>704</td>\n",
       "      <td>704</td>\n",
       "      <td>704</td>\n",
       "      <td>...</td>\n",
       "      <td>704</td>\n",
       "      <td>704</td>\n",
       "      <td>704</td>\n",
       "      <td>704</td>\n",
       "      <td>704</td>\n",
       "      <td>704</td>\n",
       "      <td>704</td>\n",
       "      <td>704</td>\n",
       "      <td>704</td>\n",
       "      <td>704</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>unique</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>2</td>\n",
       "      <td>12</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>67</td>\n",
       "      <td>2</td>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>top</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>m</td>\n",
       "      <td>White-European</td>\n",
       "      <td>no</td>\n",
       "      <td>no</td>\n",
       "      <td>'United States'</td>\n",
       "      <td>no</td>\n",
       "      <td>4</td>\n",
       "      <td>'18 and more'</td>\n",
       "      <td>Self</td>\n",
       "      <td>NO\\n</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>freq</th>\n",
       "      <td>508</td>\n",
       "      <td>385</td>\n",
       "      <td>382</td>\n",
       "      <td>355</td>\n",
       "      <td>353</td>\n",
       "      <td>504</td>\n",
       "      <td>410</td>\n",
       "      <td>457</td>\n",
       "      <td>476</td>\n",
       "      <td>404</td>\n",
       "      <td>...</td>\n",
       "      <td>367</td>\n",
       "      <td>233</td>\n",
       "      <td>635</td>\n",
       "      <td>613</td>\n",
       "      <td>113</td>\n",
       "      <td>692</td>\n",
       "      <td>131</td>\n",
       "      <td>704</td>\n",
       "      <td>522</td>\n",
       "      <td>515</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4 rows × 21 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       A1_Score A2_Score A3_Score A4_Score A5_Score A6_Score A7_Score  \\\n",
       "count       704      704      704      704      704      704      704   \n",
       "unique        2        2        2        2        2        2        2   \n",
       "top           1        0        0        0        0        0        0   \n",
       "freq        508      385      382      355      353      504      410   \n",
       "\n",
       "       A8_Score A9_Score A10_Score    ...    gender       ethnicity jundice  \\\n",
       "count       704      704       704    ...       704             704     704   \n",
       "unique        2        2         2    ...         2              12       2   \n",
       "top           1        0         1    ...         m  White-European      no   \n",
       "freq        457      476       404    ...       367             233     635   \n",
       "\n",
       "       austim    contry_of_res used_app_before result       age_desc relation  \\\n",
       "count     704              704             704    704            704      704   \n",
       "unique      2               67               2     11              1        6   \n",
       "top        no  'United States'              no      4  '18 and more'     Self   \n",
       "freq      613              113             692    131            704      522   \n",
       "\n",
       "       Class/ASD  \n",
       "count        704  \n",
       "unique         2  \n",
       "top         NO\\n  \n",
       "freq         515  \n",
       "\n",
       "[4 rows x 21 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "#f=1,m=2\n",
    "def gender2num(str):\n",
    "    if(str=='f'):\n",
    "        num = 1\n",
    "    else:\n",
    "        num = 2\n",
    "    return num\n",
    "\n",
    "df['gender'] = df['gender'].apply(gender2num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    704.000000\n",
       "mean       5.204545\n",
       "std        3.910184\n",
       "min        1.000000\n",
       "25%        1.000000\n",
       "50%        4.000000\n",
       "75%       10.000000\n",
       "max       10.000000\n",
       "Name: ethnicity, dtype: float64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#将10个不同种族替换为数值\n",
    "ethnicity = {'White-European':1,\n",
    "             'Latino':2,\n",
    "             'Black':3,\n",
    "             'Asian':4,\n",
    "             'Middle Eastern':5,\n",
    "             'Pasifika':6,\n",
    "             'South Asian':7,\n",
    "             'Hispanic':8,\n",
    "             'Turkish':9,\n",
    "             'Others':10}\n",
    "def ethnicity2num(str):\n",
    "    if str in ethnicity:\n",
    "        return ethnicity[str]\n",
    "    else: \n",
    "        return 10\n",
    "\n",
    "df['ethnicity'] = df['ethnicity'].apply(ethnicity2num)\n",
    "df['ethnicity'].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "#no=0,yes=1\n",
    "def jundice2num(str):\n",
    "    if(str=='no'):\n",
    "        num = 0\n",
    "    else: \n",
    "        if(str=='yes'):\n",
    "            num = 1\n",
    "    return num\n",
    "\n",
    "df['jundice'] = df['jundice'].apply(jundice2num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "#no=0,yes=1\n",
    "def austim2num(str):\n",
    "    if(str=='no'):\n",
    "        num = 0\n",
    "    else: \n",
    "        if(str=='yes'):\n",
    "            num = 1\n",
    "    return num\n",
    "\n",
    "df['austim'] = df['austim'].apply(austim2num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0             'United States'\n",
       "1                      Brazil\n",
       "2                       Spain\n",
       "3             'United States'\n",
       "4                       Egypt\n",
       "5             'United States'\n",
       "6             'United States'\n",
       "7               'New Zealand'\n",
       "8             'United States'\n",
       "9                     Bahamas\n",
       "10            'United States'\n",
       "11                    Burundi\n",
       "12                    Bahamas\n",
       "13                    Austria\n",
       "14                  Argentina\n",
       "15              'New Zealand'\n",
       "16                     Jordan\n",
       "17                    Ireland\n",
       "18     'United Arab Emirates'\n",
       "19     'United Arab Emirates'\n",
       "20     'United Arab Emirates'\n",
       "21     'United Arab Emirates'\n",
       "22                Afghanistan\n",
       "23     'United Arab Emirates'\n",
       "24                    Lebanon\n",
       "25                Afghanistan\n",
       "26     'United Arab Emirates'\n",
       "27                Afghanistan\n",
       "28              'New Zealand'\n",
       "29           'United Kingdom'\n",
       "                ...          \n",
       "674          'Czech Republic'\n",
       "675           'United States'\n",
       "676          'United Kingdom'\n",
       "677                  Ethiopia\n",
       "678           'United States'\n",
       "679               Afghanistan\n",
       "680          'United Kingdom'\n",
       "681           'United States'\n",
       "682           'United States'\n",
       "683           'United States'\n",
       "684           'United States'\n",
       "685                   Belgium\n",
       "686           'United States'\n",
       "687           'United States'\n",
       "688                    Canada\n",
       "689                    Canada\n",
       "690                     India\n",
       "691           'United States'\n",
       "692           'United States'\n",
       "693          'United Kingdom'\n",
       "694           'United States'\n",
       "695                    Brazil\n",
       "696                 Australia\n",
       "697               Philippines\n",
       "698                 Australia\n",
       "699                    Russia\n",
       "700                    Mexico\n",
       "701                    Russia\n",
       "702                  Pakistan\n",
       "703                    Cyprus\n",
       "Name: contry_of_res, Length: 704, dtype: object"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pop('contry_of_res')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(704, 20)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "#no=0,yes=1\n",
    "def used_app_before2num(str):\n",
    "    if(str=='no'):\n",
    "        num = 0\n",
    "    else: \n",
    "        if(str=='yes'):\n",
    "            num = 1\n",
    "    return num\n",
    "\n",
    "df['used_app_before'] = df['used_app_before'].apply(used_app_before2num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    704.000000\n",
       "mean       0.017045\n",
       "std        0.129533\n",
       "min        0.000000\n",
       "25%        0.000000\n",
       "50%        0.000000\n",
       "75%        0.000000\n",
       "max        1.000000\n",
       "Name: used_app_before, dtype: float64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['used_app_before'].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      '18 and more'\n",
       "1      '18 and more'\n",
       "2      '18 and more'\n",
       "3      '18 and more'\n",
       "4      '18 and more'\n",
       "5      '18 and more'\n",
       "6      '18 and more'\n",
       "7      '18 and more'\n",
       "8      '18 and more'\n",
       "9      '18 and more'\n",
       "10     '18 and more'\n",
       "11     '18 and more'\n",
       "12     '18 and more'\n",
       "13     '18 and more'\n",
       "14     '18 and more'\n",
       "15     '18 and more'\n",
       "16     '18 and more'\n",
       "17     '18 and more'\n",
       "18     '18 and more'\n",
       "19     '18 and more'\n",
       "20     '18 and more'\n",
       "21     '18 and more'\n",
       "22     '18 and more'\n",
       "23     '18 and more'\n",
       "24     '18 and more'\n",
       "25     '18 and more'\n",
       "26     '18 and more'\n",
       "27     '18 and more'\n",
       "28     '18 and more'\n",
       "29     '18 and more'\n",
       "           ...      \n",
       "674    '18 and more'\n",
       "675    '18 and more'\n",
       "676    '18 and more'\n",
       "677    '18 and more'\n",
       "678    '18 and more'\n",
       "679    '18 and more'\n",
       "680    '18 and more'\n",
       "681    '18 and more'\n",
       "682    '18 and more'\n",
       "683    '18 and more'\n",
       "684    '18 and more'\n",
       "685    '18 and more'\n",
       "686    '18 and more'\n",
       "687    '18 and more'\n",
       "688    '18 and more'\n",
       "689    '18 and more'\n",
       "690    '18 and more'\n",
       "691    '18 and more'\n",
       "692    '18 and more'\n",
       "693    '18 and more'\n",
       "694    '18 and more'\n",
       "695    '18 and more'\n",
       "696    '18 and more'\n",
       "697    '18 and more'\n",
       "698    '18 and more'\n",
       "699    '18 and more'\n",
       "700    '18 and more'\n",
       "701    '18 and more'\n",
       "702    '18 and more'\n",
       "703    '18 and more'\n",
       "Name: age_desc, Length: 704, dtype: object"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pop('age_desc')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    704.000000\n",
       "mean       1.781250\n",
       "std        1.478298\n",
       "min        1.000000\n",
       "25%        1.000000\n",
       "50%        1.000000\n",
       "75%        2.000000\n",
       "max        5.000000\n",
       "Name: relation, dtype: float64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#将亲属关系替换为数值\n",
    "relation = {'Self':1,\n",
    "            'Parent':2,\n",
    "            'Health care professional':3,\n",
    "            'Relative':4,\n",
    "            'Others':5}\n",
    "def relation2num(str):\n",
    "    if str in relation:\n",
    "        return relation[str]\n",
    "    else: \n",
    "        return 5\n",
    "\n",
    "df['relation'] = df['relation'].apply(relation2num)\n",
    "df['relation'].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A1_Score</th>\n",
       "      <th>A2_Score</th>\n",
       "      <th>A3_Score</th>\n",
       "      <th>A4_Score</th>\n",
       "      <th>A5_Score</th>\n",
       "      <th>A6_Score</th>\n",
       "      <th>A7_Score</th>\n",
       "      <th>A8_Score</th>\n",
       "      <th>A9_Score</th>\n",
       "      <th>A10_Score</th>\n",
       "      <th>age</th>\n",
       "      <th>gender</th>\n",
       "      <th>ethnicity</th>\n",
       "      <th>jundice</th>\n",
       "      <th>austim</th>\n",
       "      <th>used_app_before</th>\n",
       "      <th>result</th>\n",
       "      <th>relation</th>\n",
       "      <th>Class/ASD</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>26</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>NO\\n</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>24</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>NO\\n</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>27</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>8</td>\n",
       "      <td>2</td>\n",
       "      <td>YES\\n</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>35</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>NO\\n</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>NO\\n</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  A1_Score A2_Score A3_Score A4_Score A5_Score A6_Score A7_Score A8_Score  \\\n",
       "0        1        1        1        1        0        0        1        1   \n",
       "1        1        1        0        1        0        0        0        1   \n",
       "2        1        1        0        1        1        0        1        1   \n",
       "3        1        1        0        1        0        0        1        1   \n",
       "4        1        0        0        0        0        0        0        1   \n",
       "\n",
       "  A9_Score A10_Score age  gender  ethnicity  jundice  austim  used_app_before  \\\n",
       "0        0         0  26       1          1        0       0                0   \n",
       "1        0         1  24       2          2        0       1                0   \n",
       "2        1         1  27       2          2        1       1                0   \n",
       "3        0         1  35       1          1        0       1                0   \n",
       "4        0         0  40       1         10        0       0                0   \n",
       "\n",
       "  result  relation Class/ASD  \n",
       "0      6         1      NO\\n  \n",
       "1      5         1      NO\\n  \n",
       "2      8         2     YES\\n  \n",
       "3      6         1      NO\\n  \n",
       "4      2         5      NO\\n  "
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
       "       0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0,\n",
       "       1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0,\n",
       "       0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0,\n",
       "       0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,\n",
       "       1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0,\n",
       "       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0,\n",
       "       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,\n",
       "       1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1,\n",
       "       1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0,\n",
       "       0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
       "       0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
       "       0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0,\n",
       "       0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1,\n",
       "       1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,\n",
       "       0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0,\n",
       "       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
       "       0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
       "       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,\n",
       "       0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0,\n",
       "       0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0,\n",
       "       0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
       "       0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0,\n",
       "       0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0,\n",
       "       1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1,\n",
       "       0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,\n",
       "       0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0,\n",
       "       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
       "       0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1,\n",
       "       0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1,\n",
       "       1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1,\n",
       "       0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1],\n",
       "      dtype=int64)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "label = df['Class/ASD']\n",
    "def label2num(str):\n",
    "    if str=='NO\\n':\n",
    "        return 0\n",
    "    else: \n",
    "        return 1\n",
    "\n",
    "label = label.apply(label2num)\n",
    "label = np.array(label)\n",
    "label"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       NO\\n\n",
       "1       NO\\n\n",
       "2      YES\\n\n",
       "3       NO\\n\n",
       "4       NO\\n\n",
       "5      YES\\n\n",
       "6       NO\\n\n",
       "7       NO\\n\n",
       "8       NO\\n\n",
       "9      YES\\n\n",
       "10     YES\\n\n",
       "11      NO\\n\n",
       "12      NO\\n\n",
       "13      NO\\n\n",
       "14      NO\\n\n",
       "15      NO\\n\n",
       "16      NO\\n\n",
       "17      NO\\n\n",
       "18      NO\\n\n",
       "19      NO\\n\n",
       "20      NO\\n\n",
       "21      NO\\n\n",
       "22      NO\\n\n",
       "23      NO\\n\n",
       "24      NO\\n\n",
       "25      NO\\n\n",
       "26      NO\\n\n",
       "27      NO\\n\n",
       "28      NO\\n\n",
       "29      NO\\n\n",
       "       ...  \n",
       "674     NO\\n\n",
       "675     NO\\n\n",
       "676     NO\\n\n",
       "677     NO\\n\n",
       "678    YES\\n\n",
       "679    YES\\n\n",
       "680    YES\\n\n",
       "681    YES\\n\n",
       "682     NO\\n\n",
       "683     NO\\n\n",
       "684     NO\\n\n",
       "685    YES\\n\n",
       "686     NO\\n\n",
       "687    YES\\n\n",
       "688    YES\\n\n",
       "689    YES\\n\n",
       "690     NO\\n\n",
       "691     NO\\n\n",
       "692    YES\\n\n",
       "693     NO\\n\n",
       "694    YES\\n\n",
       "695     NO\\n\n",
       "696    YES\\n\n",
       "697     NO\\n\n",
       "698    YES\\n\n",
       "699    YES\\n\n",
       "700     NO\\n\n",
       "701    YES\\n\n",
       "702     NO\\n\n",
       "703    YES\\n\n",
       "Name: Class/ASD, Length: 704, dtype: object"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pop('Class/ASD')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(704, 19)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#增加一列数据，全为1，对应参数b\n",
    "df['X_b']=1\n",
    "data = np.array(df)\n",
    "data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A1_Score           object\n",
       "A2_Score           object\n",
       "A3_Score           object\n",
       "A4_Score           object\n",
       "A5_Score           object\n",
       "A6_Score           object\n",
       "A7_Score           object\n",
       "A8_Score           object\n",
       "A9_Score           object\n",
       "A10_Score          object\n",
       "age                object\n",
       "gender              int64\n",
       "ethnicity           int64\n",
       "jundice             int64\n",
       "austim              int64\n",
       "used_app_before     int64\n",
       "result             object\n",
       "relation            int64\n",
       "X_b                 int64\n",
       "dtype: object"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\ProgramFiles\\Anaconda\\lib\\site-packages\\ipykernel_launcher.py:4: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
      "  after removing the cwd from sys.path.\n"
     ]
    }
   ],
   "source": [
    "#用众数填充年龄中的问号\n",
    "for i in range(704):\n",
    "    if(df['age'][i]=='?'):\n",
    "        df['age'][i] = 21\n",
    "df['age'] = df['age'].astype(float)        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df.astype(float)   \n",
    "data = np.array(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "#训练集\n",
    "data_train = data[:500]\n",
    "label_train = label[:500]\n",
    "#测试集\n",
    "data_predict = data[500:]\n",
    "label_predict = label[500:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "#初始化w，包含了b（w；b）\n",
    "w0 = np.array(np.zeros(data[0].shape))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "代价函数：西瓜书公式3.27\n",
    "\n",
    "@para   data：数据集\n",
    "@para   label：标签\n",
    "@para   w：权值加上偏置（w；b）\n",
    "@return loat：当前权值输入下计算得到的误差损失\n",
    "'''\n",
    "def cost_f(data,label,w):\n",
    "    lost = 0.0\n",
    "    for i in range(data.shape[0]):\n",
    "        x = data[i]\n",
    "        lost = lost+ (-label[i] * np.dot(w.T,x) + np.log(1 + np.exp(np.dot(w.T,x)))) \n",
    "    return lost\n",
    "'''\n",
    "西瓜书公式3.23，计算后验概率估计p（y=1|x）\n",
    "\n",
    "@para   x：数据集\n",
    "@para   w：权值加上偏置（w；b）\n",
    "@return p1\n",
    "'''\n",
    "def p1(x, w):\n",
    "    temp = np.exp(np.dot(w.T,x))\n",
    "    return temp/(1+temp)\n",
    "'''\n",
    "#西瓜书公式3.30，计算损失函数的一阶导\n",
    "\n",
    "@para   data：数据集\n",
    "@para   label：标签\n",
    "@para   w：权值加上偏置（w；b）\n",
    "@return w_1d：损失函数的一阶导\n",
    "'''\n",
    "def cost_f_1differential(w, data, label):\n",
    "    w_1d = np.array(np.zeros(data[0].shape))\n",
    "    for i in range(data.shape[0]):  \n",
    "        x = data[i]\n",
    "        y = label[i]\n",
    "        w_1d += -np.dot(x, (y - p1(x, w)))\n",
    "    return w_1d;\n",
    "'''\n",
    "#西瓜书公式3.31，计算损失函数的二阶导\n",
    "\n",
    "@para   data：数据集\n",
    "@para   w：权值加上偏置（w；b）\n",
    "@return w_2d：损失函数的二阶导\n",
    "'''\n",
    "def cost_f_2differential(w, data):\n",
    "    w_2d = 0.0\n",
    "    for i in range(data.shape[0]):\n",
    "        x = data[i]\n",
    "        w_2d += np.dot(x,x.T)*(1 - p1(x, w))*p1(x, w)\n",
    "    return w_2d;\n",
    "'''\n",
    "#牛顿法迭代计算似然函数最小值\n",
    "\n",
    "@para   data：数据集\n",
    "@para   label：标签\n",
    "@para   w：权值加上偏置（w；b）\n",
    "@return w：更新后的w\n",
    "'''\n",
    "def newton_method(w, data, label):\n",
    "    cost_last = 0\n",
    "    cost= 1\n",
    "    i =0\n",
    "    while(np.abs(cost-cost_last)>0.0001):\n",
    "        cost_last = cost\n",
    "        w_1d = cost_f_1differential(w, data, label)\n",
    "        w_2d = cost_f_2differential(w, data)\n",
    "        w = w - w_1d/w_2d\n",
    "        cost = cost_f(data,label,w)\n",
    "        i=i+1\n",
    "        print('period:',i,' cost is:',cost)\n",
    "    return w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "梯度下降更新w\n",
    "\n",
    "@para   data：数据集\n",
    "@para   label：标签\n",
    "@para   w：权值加上偏置（w；b）\n",
    "@para   period：周期\n",
    "@return w：更新后的w\n",
    "'''\n",
    "def GD(w, data, label,period):\n",
    "    a = 0.0001   #学习率\n",
    "    cost_last = 0\n",
    "    for i in range(period):\n",
    "        w = w - a * cost_f_1differential(w, data, label)\n",
    "        cost = cost_f(data,label,w)\n",
    "        #print('period:',i,' cost is:',cost)\n",
    "        if(np.abs(cost-cost_last)<0.00001):\n",
    "            return w\n",
    "    return w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "#牛顿法计算，返回最优解\n",
    "#w1 = newton_method(w0, data_train, label_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "#梯度下降法计算\n",
    "w2 = GD(w0, data_train, label_train, 2000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.07843137])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "目标函数\n",
    "\n",
    "@para   data：数据集\n",
    "@para   w：权值加上偏置（w；b）\n",
    "@return 1/(1+np.exp(-z))：目标函数值\n",
    "'''\n",
    "def object_f(w,data):\n",
    "    z = np.dot(w,data.T)\n",
    "    return 1/(1+np.exp(-z))\n",
    "'''\n",
    "计算错误率:阈值为0.5；计算预测值y与标签label不同的个数比例\n",
    "\n",
    "@para   y：预测值\n",
    "@para   label：标签\n",
    "@return err：错误率\n",
    "'''\n",
    "#计算错误率:阈值为0.5；计算预测值y与标签label不同的个数比例\n",
    "def err(y,label):\n",
    "    y = [1 if(x>0.5)else 0 for x in y]\n",
    "    return np.abs(y-label).sum()/label.shape\n",
    "\n",
    "e = err(object_f(w2,data_predict),label_predict)\n",
    "\n",
    "e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "period: 0  err is: [0.07142857]\n",
      "period: 1  err is: [0.05714286]\n",
      "period: 2  err is: [0.07142857]\n",
      "period: 3  err is: [0.05714286]\n",
      "period: 4  err is: [0.07142857]\n",
      "period: 5  err is: [0.01428571]\n",
      "period: 6  err is: [0.02857143]\n",
      "period: 7  err is: [0.07142857]\n",
      "period: 8  err is: [0.05714286]\n",
      "period: 9  err is: [0.11428571]\n"
     ]
    }
   ],
   "source": [
    "#10折交叉验证\n",
    "#np.random.shuffle(data)\n",
    "data = data[:700]\n",
    "label = label[:700]\n",
    "data_len = data.shape[0]\n",
    "err_10_fold = []\n",
    "\n",
    "for i in range(10):\n",
    "    data_test = data[i*70:(i+1)*70]\n",
    "    label_test = label[i*70:(i+1)*70]\n",
    "    if i>0&i<9 :\n",
    "        data_train = np.concatenate([data[:i*70], data[(i+1)*70:]],axis=0)\n",
    "        label_train = np.concatenate([label[:i*70], label[(i+1)*70:]],axis=0)\n",
    "    else:\n",
    "        if i==0 :\n",
    "            data_train = data[70:]\n",
    "            label_train = label[70:]\n",
    "        if i==9 :\n",
    "            data_train = data[630:]\n",
    "            label_train = label[630:]\n",
    "    w = GD(w0, data_train, label_train, 2000)\n",
    "    e = err(object_f(w,data_test),label_test)\n",
    "    print('period:',i,' err is:',e)\n",
    "    err_10_fold.append(e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.06142857142857143"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(err_10_fold)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
